package com.itboye.ihomebank.activity.key;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2018/9/13 0013.
 * 转移管理员
 */

public class ActivityTransAdmin extends BaseOtherActivity  implements Observer{
    View v_statusbar;
    private ImageView close_icon;// 返回图片
    private TextView add_shap_title_tv;// 标题

    private TextView confirmTv; //下一步
    private EditText phoneEt; //接受管理员的电话

    private String uid;
    private String lock_id;//锁id
    private UserPresenter userPresenter;
    @Override
    protected int layout_id() {
        return R.layout.activity_transadmin;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("转移智能锁");

        lock_id=getIntent().getStringExtra("lock_id");
        uid= SPUtils.get(this,null, SPContants.USER_ID,"")+"";
        userPresenter=new UserPresenter(this);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){

            case R.id.close_icon:
                this.finish();
                break;

            case R.id.confirmTv:
                //调用转移管理员接口
                confirmTv.setEnabled(false);
                if (phoneEt.getText().toString().equals("")||phoneEt.getText()==null){
                    ByAlert.alert("请填写接受管理员的电话号码");
                    confirmTv.setEnabled(true);
                    return;
                }
                showProgressDialog("正在转移锁...",false);
                userPresenter.changeAdministrator(uid,lock_id,phoneEt.getText().toString().trim());
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        try{
            closeProgressDialog();
        }catch (Exception e){

        }
        ResultEntity entity = handlerError(data);
        if (entity!=null){

            if (entity.getEventType()==UserPresenter.BY_Sciener2_changeAdmin_success){
                confirmTv.setEnabled(true);
                ByAlert.alert("管理员转移成功");
            }else if (entity.getEventType() == UserPresenter.BY_Sciener2_changeAdmin_fail){
                confirmTv.setEnabled(true);
                ByAlert.alert(entity.getMsg());
            }
        }
    }
}
